package com.weitu.workflow.flowManagement.tag;

import com.weitu.accessory.bean.Accessory;
import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.workflow.flowManagement.entity.WorkflowReview;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import java.util.List;

/**
 * 流程业务审批意见自定义标签类
 * 用于填写处理意见
 * Created by fanyuting on 2017/3/13 0013.
 */
public class WorkflowReviewTag extends TagSupport {
    private PageContext pageContext;
    //private String businessId;
    private String workItemId;//工作项id
    private String requestName;//审批意见的requestName
    public WorkflowReviewTag() {}

    @Override
    public void setPageContext(PageContext pageContext) {
        this.pageContext = pageContext;
    }

    @Override
    public int doStartTag() throws JspException {
        HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
        WorkflowReview workflowReview=(WorkflowReview) request.getAttribute(requestName);//意见信息
        /*if (!(workflowReview.getIsDynamicAssigner()==1)){
            return SKIP_BODY;
        }*/
        try {
        //查询出对应业务Id和附件包Id的附件信息

            JspWriter out = this.pageContext.getOut();

            StringBuffer reviewBuffer=new StringBuffer();
            /*reviewBuffer.append("<div class='flow_review'>");
            reviewBuffer.append("<div class='review_result'>");
            reviewBuffer.append("<label><input name='"+this.requestName+".result' type='radio' value='1'/>同意</label>");
            reviewBuffer.append("<label><input name='"+this.requestName+".result' type='radio' value='0'/>不同意</label>");
            reviewBuffer.append("</div>");
            reviewBuffer.append("<div class='review_comments'>");
            reviewBuffer.append("<textarea rows='7' name='"+this.requestName+".comments'></textarea>");
            reviewBuffer.append("</div>");
            reviewBuffer.append("</div>");*/
            reviewBuffer.append("<div class='flow_review'>"
                    +"<table>"
                    +"<tr>"
                    +"<td class=\"review_title\">处理结果：</td>"
                    +"<td><label><input name='"+this.requestName+".result' type='radio' value='1'/>同意</label><label><input name='"+this.requestName+".result' type='radio' value='0'/>不同意</label></td>"
                    +"</tr>"
                    +"<tr>"
                    +"<td class=\"review_title\">处理意见：</td>"
                    +"<td><textarea name='"+this.requestName+".comments' rows=\"7\"></textarea></td>"
                    +"</tr>"
                    +"<tr>"
            );
            if (workflowReview.getIsDynamicAssigner()==1){
                reviewBuffer.append("<td class=\"review_title\">是否继续处理：</td>"
                        +"<td><label class=\"review_next_check\"><input name='"+this.requestName+".isDynamicAssigner' type='checkbox' value='1' onclick=\"setAssignerNextState(this,'"+this.requestName+"');\"/>指定下一环节处理人</label></td>"
                        +"</tr>"
                        +"<tr id='"+this.requestName+".nextUserSelectDiv' style='display:none;'>"
                        +"<td class=\"review_title\">下一环节处理人：</td>"
                        +"<td><input type=\"hidden\" name='" + this.requestName + ".nextActivityId' value='"+workflowReview.getNextActivityId()+"'/><input class=\"easyui-textbox\" style=\"width:150px;\" id='nextActivityUserId' name='"+this.requestName+".nextUserId' type=\"text\" value=\"\"/><a onclick=\"openAssignerSelectUserPage('"+this.requestName+"');\">选择人员</a></td>"
                        +"</tr>"
                );
            }
            reviewBuffer.append("</table>"
                            +"</div>"
            );

            out.print(reviewBuffer);
/*
            AccessoryPackageInfo packageInfo = new AccessoryPackageInfo();
            //循环遍历所有的附件，显示满足附件包KEY的信息
            List<AccessoryPackageInfo> list = (List<AccessoryPackageInfo>) packageInfos;
            for(AccessoryPackageInfo p: list){
                if(p.getAccessoryPackage().getPackageKey().equals(packageKey)){
                    packageInfo = p;
                }
            }
            out.print("<div class='accessory_div' id='accessory_div' >");
            //存放限制的文件类型
            if(accessoryType==null || "".equals(accessoryType)){
                accessoryType="txt,docx,xls,xlsx,jpg,png";
            }
            out.print("<input id='accessoryType' type='hidden' value=\""+accessoryType+"\"/>");
            if(permission!=null &&permission!="" &&"write".equals(permission)){
                out.print("<a class='accessory_selectBtn' href='javascript:void(0);'  onclick=\"accessoryOpenFile('"+packageKey+"','"+uploadUserId+"');\">添加附件</a>");
            }

            out.println("<table class='accessory_table' id="+packageKey+"accessoryTable width='500px' border='1' align='center'>");

            out.println("<tr>");

            out.println("<td width='25%'>附件名称:</td><td width='25%'>附件大小</td><td width='25%'>上传时间</td><td width='25%'>操作</td>");
            out.println("</tr>");
            if(packageInfo!=null&&packageInfo.getAccessorys()!=null && packageInfo.getAccessorys().size()>0){
                String packageKey =packageInfo.getAccessoryPackage().getPackageKey();
                for(Accessory accessory: packageInfo.getAccessorys()){
                    String accessoryId = accessory.getId();

                    out.print("<tr id=\"" + accessoryId + "_tr\">");

                    out.print("<td width='25%'>"+accessory.getFileName()+"</td><td width='25%'>"+accessory.getFileSize()+"</td><td width='25%'>"+accessory.getUploadTime()+"</td>" +
                            "<td width='25%'>" );
                    //权限判断是否可编辑
                    String accessoryUserId = accessory.getUserId();
                    if(permission!=null &&permission!="" &&"write".equals(permission)){
                        if(uploadUserId!=null&&accessoryUserId!=null && uploadUserId.equals(accessoryUserId)){
                            out.print("<a class='accessory_deleteBtn'  href='javascript:void(0);' onclick=\"accessoryDeleteAccessory('" + accessoryId + "','"+packageKey+"');\" >删除</a>");
                        }
                    }
                    out.print("<a class='accessory_downloadBtn'  href='javascript:void(0);' onclick=\"accessoryDownloadAccessory('" + accessoryId+ "');\" />下载</a>");


                    out.print("</td>");
                    out.print("</tr>");
                }
            }

            out.println("</table>");
            out.print("<div id=\"progress\">\n" +
                    "    　<div class=\"bar\" style=\"width:0%;height:20px; background-color: #1f637b;\"></div>\n" +
                    "</div>");
            out.print("</div>");*/

        } catch(Exception e) {

            throw new JspException(e.getMessage());

        }

        return SKIP_BODY;
    }

    @Override
    public int doEndTag() throws JspException {
        return super.doEndTag();
    }

    public String getRequestName() {
        return requestName;
    }

    public void setRequestName(String requestName) {
        this.requestName = requestName;
    }

    public PageContext getPageContext() {
        return pageContext;
    }

    public String getWorkItemId() {
        return workItemId;
    }

    public void setWorkItemId(String workItemId) {
        this.workItemId = workItemId;
    }
}
